package com.neusoft.news.dao;

import com.neusoft.news.bean.Clue;
import com.neusoft.news.utils.JDBCUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 线索数据库操作
 *
 * @author wh
 * @ClassName: ClueDao
 * @Description: 线索数据库操作
 * @date 2021年5月23日
 * @since JDK 1.8
 */
public class ClueDao {
    /**
     * 线索列表
     *
     * @param state 1:不包括废弃
     * @return 线索列表
     * @Title: findAllClue
     * @Description: 线索列表
     */
    public List<Clue> findAllClue(int state) {
        ArrayList<Clue> clues = new ArrayList<>();
        try {
            String sql = "select * from \"d_clue\""
                    + (state == 1 ? "where \"clue_state!=\"" + Clue.INVALID : "");
            PreparedStatement pst = JDBCUtils.getConnection().prepareStatement(sql);
            ResultSet query = pst.executeQuery();
            while (query.next()) {
                Clue clue = new Clue(query.getString(1),
                        query.getString(2),
                        query.getString(3),
                        query.getString(4),
                        query.getDate(5),
                        query.getString(6),
                        query.getDate(7));
                clues.add(clue);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return clues;
    }

    /**
     * 线索新增
     *
     * @param clue 线索
     * @return 新增条数
     * @Title: addClue
     * @Description: 新增
     */
    public int addClue(Clue clue) {
        int i = 0;
        try {
            String sql = "insert into \"d_clue\" values(?,?,?,?,TO_DATE(?, 'yyyy-MM-dd hh24:mi:ss'),?,?)";
            PreparedStatement pst = JDBCUtils.getConnection().prepareStatement(sql);
            pst.setString(1, clue.getClueId());
            pst.setString(2, clue.getClueContent());
            pst.setString(3, clue.getClueState() == null ? "0" : clue.getClueState());
            pst.setString(4, clue.getClueReporter());
            pst.setString(5, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            pst.setString(6, null);
            pst.setString(7, null);

            i = pst.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return i;
    }

    /**
     * 更新线索状态
     *
     * @param clueId 线索id
     * @param state  线索状态
     * @return 变更条数
     * @Title: updateClueState
     * @Description: 更新线索状态
     */
    public int updateClueState(String clueId, String state) {
        int i = 0;
        try {
            String sql = "update \"d_clue\" set \"clue_state\"=? where \"clue_id\"=?";
            PreparedStatement pst = JDBCUtils.getConnection().prepareStatement(sql);
            pst.setString(1, state);
            pst.setString(2, clueId);

            i = pst.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return i;
    }

    /**
     * 指派线索追踪
     *
     * @param clueId   线索id
     * @param verifier 指派线索追踪人
     * @return 更改条数
     * @Title: assignmentTracking
     * @Description: 指派线索追踪
     */
    public int assignmentTracking(String clueId, String verifier) {
        int i = 0;
        try {
            String sql = "update \"d_clue\" set \"clue_verifier\"=?,\"clue_verify_time\"=TO_DATE(?, 'yyyy-MM-dd hh24:mi:ss') where \"clue_id\"=?";
            PreparedStatement pst = JDBCUtils.getConnection().prepareStatement(sql);
            pst.setString(1, verifier);
            pst.setString(2, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            pst.setString(3, clueId);

            i = pst.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

}
